<template>
  <div>
    <div style="margin-bottom: 30px">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>公司公告管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <h1>公告列表</h1>
    <el-divider></el-divider>
    <el-button type="primary" @click="add" class="mb-5"> 添加公告<i class="el-icon-plus"></i></el-button>
    <el-card class="box-card" v-for="notice in notices" shadow="hover">
      <div>
        <span>{{ notice.noticeTitle }}</span>
        <el-button type="success" style="margin-left: 100px" @click="edit(notice)" class="mr-5">编辑
          <i class="el-icon-edit"></i></el-button>
        <el-popconfirm
            confirm-button-text='确定'
            cancel-button-text='再想想'
            icon="el-icon-info"
            icon-color="red"
            title="确定删除吗？"
            @confirm="del(notice.noticeId)">
          <el-button type="danger" slot="reference">删除<i class="el-icon-delete"></i></el-button>
        </el-popconfirm>
        <br>
        <span style="font-size: 12px">发布时间：{{ notice.noticeTime }}</span>
      </div>
    </el-card>
    <el-dialog
        title="公告信息"
        :visible.sync="dialogVisible"
        width="30%">
      <el-form label-width="80px" :model="notice" :rules="rules" ref="notice">
        <el-form-item label="公告标题">
          <el-input v-model="notice.noticeTitle"></el-input>
        </el-form-item>
        <el-form-item label="公告内容">
          <el-input v-model="notice.noticeText" type="textarea" :rows="10"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save('notice')">{{ btnText }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "notice",
  data() {
    return {
      notices: [],
      notice: {},
      dialogVisible:false,
      btnText:'',
      rules:{}
    }
  },
  created() {
    this.load()
  },
  methods:{
    load() {
      request.get("/notice/getAll").then(res => {
        this.notices = res.data.records
      })
    },
    add(){
      this.notice={}
      this.dialogVisible=true
      this.btnText='添加'
    },
    edit(notice){
      this.notice=Object.assign({},notice)
      this.notice.noticeText = this.notice.noticeText.replace(/<br\/>/g, '\n').replace(/&nbsp;/g, '\s')
      this.dialogVisible=true
      this.btnText='修改'
    },
    del(id){
      request.delete("notice/delmore/" + id).then(res => {
        if (res.code === 200) {
          this.$message.success("删除成功")
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    save(notice) {
      this.$refs[notice].validate((valid) => {
        if (valid) {
          this.dialogVisible = false
          this.notice.noticeText = this.notice.noticeText.replace(/\r\n/g, '<br/>').replace(/\n/g, '<br/>').replace(/\s/g, '&nbsp;');
          if (this.btnText==='添加'){
            request.post("notice/add", this.notice).then(res => {
              if (res) {
                this.$message.success(this.btnText + "成功")
                this.load()
              } else {
                this.$message.error(this.btnText + "失败")
              }
            })
          }else{
            request.put("notice/update", this.notice).then(res => {
              if (res) {
                this.$message.success(this.btnText + "成功")
                this.load()
              } else {
                this.$message.error(this.btnText + "失败")
              }
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
  }
}
</script>

<style scoped>
.box-card {
  margin: 20px auto;
  width: 80%;
  height: 100px;
  font-size: 20px;
}
</style>